﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TfsWorkItemTracking = Microsoft.TeamFoundation.WorkItemTracking.Client;
using FunctionPoint.Services;
using Common = FunctionPoint.Common;
using System.Collections.ObjectModel;
using FunctionPoint.UIManagers;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Shell;

namespace FunctionPoint
{
    public static class Utilities
    {
        

        public static string GetFilePath(this Microsoft.VisualStudio.Text.Editor.IWpfTextView wpfTextView)
        {
            Microsoft.VisualStudio.Text.ITextDocument document;
            if ((wpfTextView == null) ||
                    (!wpfTextView.TextDataModel.DocumentBuffer.Properties.TryGetProperty(typeof(Microsoft.VisualStudio.Text.ITextDocument), out document)))
                return String.Empty;

            // If we have no document, just ignore it. 
            if ((document == null) || (document.TextBuffer == null))
                return String.Empty;

            return document.FilePath;
        }

        public static Common.WorkItem CreateCommonWorkItem(this TfsWorkItemTracking.WorkItem tfsWorkItem)
        {
            if (!string.IsNullOrEmpty(tfsWorkItem.Title))
            {
                Common.WorkItem workItem = new Common.WorkItem()
                {
                    WorkItemId = tfsWorkItem.Id,
                    Description = tfsWorkItem.Description,
                    Title = tfsWorkItem.Title,
                    WorkItemType = tfsWorkItem.Fields[TfsWorkItemTracking.CoreField.WorkItemType].Value.ToString(),
                    PrimaryEngineer = tfsWorkItem.Fields[TfsWorkItemTracking.CoreField.AssignedTo].Value.ToString(),
                    FunctionPointTitle = tfsWorkItem.Title,


                };

                if (tfsWorkItem.Fields.Contains(Common.Constants.TFS_FIELD_DESIGNPOINT_CODEELEMENT_CLOSESTCHARACTERPOSITION))
                    workItem.ClosestCharacterPosition = Convert.ToInt32(tfsWorkItem.Fields[Common.Constants.TFS_FIELD_DESIGNPOINT_CODEELEMENT_CLOSESTCHARACTERPOSITION].Value);

                if (tfsWorkItem.Fields.Contains(Common.Constants.TFS_FIELD_DESIGNPOINT_FILEPATH))
                    workItem.ItemPath = tfsWorkItem.Fields[Common.Constants.TFS_FIELD_DESIGNPOINT_FILEPATH].Value as string;

                if (tfsWorkItem.Fields.Contains(Common.Constants.TFS_FIELD_DESIGNPOINT_DESIGNERNAME))
                    workItem.SolutionDesigner = tfsWorkItem.Fields[Common.Constants.TFS_FIELD_DESIGNPOINT_DESIGNERNAME].Value as string;

                return workItem;
            }
            return null;
        }

        public static void AddRange<T>(this Collection<T> collection, IEnumerable<T> items)
        {
            foreach (T item in items)
                collection.Add(item);

        }

        public static UIManagers.WpfTextViewAdornmentManager GetOrCreateManagerForTextView(this IWpfTextView textView)
        {
            return textView.Properties.GetOrCreateSingletonProperty<WpfTextViewAdornmentManager>(() =>
                new WpfTextViewAdornmentManager(textView, ServiceProvider.GlobalProvider));
        }
    }
}
